#include "material_base.h"
#include "light.h"
#include "ray.h"
#include "scene.h"

std::map<std::string, Material*> Material::s_materials;

Material::Material(Scene *scene) : m_scene(scene)
{

}

Material::~Material()
{
}

void Material::cache(std::string name, Material *m)
{
  s_materials[name] = m;
}

Material *Material::getMaterial(std::string materialName)
{
  return s_materials.at(materialName);
}

void Material::m_castTransparentRay(Collision c, double ior, Color &targetColor)
{
  double refractionAmount = 1;
  Ray refractRay;

  c.src.normalize();
  refractRay.pos = c.pos + c.src * 0.01;
  refractRay.dir = c.src;
  refractRay.ior = ior;
  refractRay.parent = 0;
  refractRay.recursionLevel = c.recursionLevel + 1;
  m_scene->cast(refractRay, targetColor);
}
void Material::m_castReflectRay(Collision c, Color &targetColor)
{
  Ray reflectRay;
  reflectRay.pos = c.pos;
  reflectRay.dir = c.normal + c.normal - c.src;
  reflectRay.parent = c.parent;
  reflectRay.recursionLevel = c.recursionLevel + 1;
  m_scene->cast(reflectRay, targetColor);
}
void Material::m_castLightRays(Collision c, Color &diffuseColor, Color &specularColor)
{
  for (LightIterator i = Light::begin(); i != Light::end(); i++)
  {
    double reflectK;
    Color tmpDiffuse, tmpSpecular;
    tmpDiffuse = (*i)->computeColor(c, reflectK);
    tmpSpecular = tmpDiffuse * reflectK;

    diffuseColor += tmpDiffuse;
    specularColor += tmpSpecular;
  }
}
